#include <QFile>
#include <QFrame>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QToolButton>
#include <QTextEdit>
#include <QToolBar>
#include <QVBoxLayout>
#include <QWebView>

#include <messaginglayer/cmessaginglayer.h>

#include "constants.h"
#include "contextmanager.h"
#include "icore.h"
#include "messagingwidget.h"
#include "uidmanager.h"

MessagingWidget::MessagingWidget(QWidget *parent) :
    QWidget(parent),
    m_currentContactLabel(new QLabel(this)),
    m_closeDialogButton(new QToolButton(this)),
    m_conversationText(new QWebView(this)),
    m_toolBar(new QToolBar(this)),
    m_outgoingMessage(new QTextEdit(this)),
    m_sendButton(new QPushButton(this))
{
    QHBoxLayout * l = new QHBoxLayout(this);
    QWidget *w = new QWidget(this);
    w->setFixedWidth(450);
    l->addSpacerItem(new QSpacerItem(20, 10, QSizePolicy::Expanding));
    l->addWidget(w);
    l->addSpacerItem(new QSpacerItem(20, 10, QSizePolicy::Expanding));

    QVBoxLayout * main = new QVBoxLayout(w);
    l->setContentsMargins(0, 0, 0, 0);
    l->setSpacing(3);

    QHBoxLayout * top = new QHBoxLayout();
    top->addWidget(m_currentContactLabel);
    top->addSpacerItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Minimum));
    top->addWidget(m_closeDialogButton);
    main->addLayout(top);

    QHBoxLayout * out = new QHBoxLayout();
    out->addWidget(m_outgoingMessage);
    out->addWidget(m_sendButton);

    QFrame * line = new QFrame(this);
    line->setFrameStyle(QFrame::HLine);
    main->addWidget(line);

    main->addWidget(m_conversationText);
    main->addSpacerItem(new QSpacerItem(10, 20));
    main->addWidget(m_toolBar);
    main->addLayout(out);

    m_closeDialogButton->setAutoRaise(true);
    m_closeDialogButton->setIcon(QIcon(":/core/images/editclear.png"));
    connect(m_closeDialogButton, SIGNAL(released()), this, SLOT(closeDialog()));

    m_sendButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    m_sendButton->setFixedWidth(80);
    m_sendButton->setIcon(QIcon(":/core/images/key-return.png"));
    m_sendButton->setIconSize(QSize(32, 32));
    connect(m_sendButton, SIGNAL(released()), this, SLOT(sendMessage()));

    m_outgoingMessage->setFixedHeight(120);
    m_toolBar->setHidden(true);
}

ContactInfo MessagingWidget::currentContact() const
{
    return m_currentContact;
}

void MessagingWidget::setCurrentContact(const ContactInfo &cinfo)
{
    m_currentContact = cinfo;

    m_conversationText->setUrl(QUrl("qrc:/core/images/history-loading.html"));
    m_currentContactLabel->setText(cinfo.name());
}

void MessagingWidget::closeDialog()
{
    ContextManager * cm = ICore::instance()->contextManager();
    UidManager * uidm = ICore::instance()->uidManager();

    m_currentContact = ContactInfo();
    m_currentContactLabel->clear();
    m_conversationText->setHtml(QString());
    m_outgoingMessage->clear();

    cm->setGuiContext(uidm->uniqueIndentifier(Constants::C_CONTACTS));
}

void MessagingWidget::sendMessage()
{
    CMessagingLayer * mlayer = ICore::instance()->messagingLayer();

    mlayer->sendMessage(m_outgoingMessage->toPlainText(), m_currentContact);
    m_outgoingMessage->clear();
}

void MessagingWidget::messagingHistoryAvaible()
{
    // text
}
